package com.iwayin.core.bo.comment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.iwayin.core.bo.BaseBO;
import com.iwayin.core.common.HistoryInfoUtil;
import com.iwayin.core.dao.ask.AskDAO;
import com.iwayin.core.dao.atinfo.AtInfoDAO;
import com.iwayin.core.dao.comment.CommentDAO;
import com.iwayin.core.dao.commentditto.CommentDittoDAO;
import com.iwayin.core.dao.user.UserDAO;
import com.iwayin.core.dataobject.AskDO;
import com.iwayin.core.dataobject.AtInfoDO;
import com.iwayin.core.dataobject.CommentDO;
import com.iwayin.core.dataobject.CommentDittoDO;
import com.iwayin.core.dataobject.UserDO;
import com.iwayin.core.utils.AtUserUtil;
import com.iwayin.core.utils.StringUtil;
import com.iwayin.core.utils.exception.BOException;

public class CommentBOImpl extends BaseBO implements CommentBO {
	private UserDAO userDAO;
	private CommentDAO commentDAO;
	private AskDAO askDAO;
	private AtInfoDAO atInfoDAO;
	private CommentDittoDAO commentDittoDAO;

	@Override
	public Long addComment(CommentDO commentDO) {
		try {
			AskDO ask = this.askDAO.findAskByAskId(commentDO.getAskId());
			commentDO.setQuestionUid(ask.getUserId());
			// 评论
			Long commentId = commentDAO.addComment(commentDO);
			// 增加ask的评论数量
			askDAO.askCommentCountPlusOne(commentDO.getAskId());
			if (StringUtil.isNotEmpty(commentDO.getCommentBody()) && commentDO.getCommentBody().indexOf("@") != -1) {
				List<String> atUserNameList = AtUserUtil.getAtUserNameList(commentDO.getCommentBody());
				List<AtInfoDO> atInfoList = new ArrayList<AtInfoDO>();
				for (String userNameStr : atUserNameList) {
					AtInfoDO atInfo = new AtInfoDO();
					atInfo.setAccountId(commentId);
					atInfo.setAtUserName(userNameStr);
					atInfo.setAtType(AtInfoDO.AT_TYPE_COMMENT);
					atInfoList.add(atInfo);
				}
				atInfoDAO.addAtInfoBatch(atInfoList);
			}
			return commentId;
		} catch (Exception e) {
			loger.error("CommentBO.addComment", e);
			throw new BOException("评论失败");
		}
	}

	@Override
	public void deleteComment(CommentDO comment) {
		this.commentDAO.deleteComment(comment);
	}

	@Override
	public List<CommentDO> findAskComments(CommentDO query) {
		List<CommentDO> askComments = this.commentDAO.findAskComments(query);
		List<CommentDO> resultAskComments = new ArrayList<CommentDO>();
		for (CommentDO askComment : askComments) {
			UserDO user = this.userDAO.getUserById(askComment.getUserId());
			if (user != null) {
				askComment.setHistoryInfo(HistoryInfoUtil.getHistoryInfo(askComment.getCreateTime()));
				askComment.setNickname(user.getNickname());
				askComment.setAvatarPath(user.getAvatarPath());
				if (query.getLoginUserId() != null) {
					CommentDittoDO commentDitto = new CommentDittoDO();
					// 判断是否喜欢此评论
					commentDitto.setCommentId(askComment.getId());
					commentDitto.setUserId(query.getLoginUserId());
					boolean isLikeComment = this.commentDittoDAO.isAddDittoComment(commentDitto);
					if (isLikeComment) {
						askComment.setLikeComment(true);
					}
				}

				List<String> atUserList = AtUserUtil.getAtUserNameList(askComment.getCommentBody());
				if (null != atUserList && atUserList.size() > 0) {
					Map<String, String> userMap = new HashMap<String, String>();
					for (String loginName : atUserList) {
						UserDO userObj = userDAO.getUserByLoginName(loginName);
						if (null != userObj) {
							userMap.put(loginName, userObj.getLoginName());
						}
					}
					// @用户转换
					askComment.setCommentBody(AtUserUtil.atUserName(askComment.getCommentBody(), userMap));
				}
				resultAskComments.add(askComment);
			}
		}
		return resultAskComments;
	}

	@Override
	public List<UserDO> findMostCommentToday(Long size) {
		List<CommentDO> comments = commentDAO.findMostCommentToday(size);
		List<UserDO> users = new ArrayList<UserDO>(comments.size());
		if (comments.size() > 0) {
			for (CommentDO c : comments) {
				UserDO user = userDAO.getUserById(c.getUserId());
				user.setCommentCountToday(c.getCommentCountToday());
				users.add(user);
			}
		}
		return users;
	}

	@Override
	public void commentDittoPlusOne(Long commentId) {
		commentDAO.commentDittoPlusOne(commentId);
	}

	@Override
	public void cancelCommentDitto(Long commentId) {
		commentDAO.cancelCommentDitto(commentId);
	}

	public void setCommentDAO(CommentDAO commentDAO) {
		this.commentDAO = commentDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public void setAskDAO(AskDAO askDAO) {
		this.askDAO = askDAO;
	}

	public void setAtInfoDAO(AtInfoDAO atInfoDAO) {
		this.atInfoDAO = atInfoDAO;
	}

	public void setCommentDittoDAO(CommentDittoDAO commentDittoDAO) {
		this.commentDittoDAO = commentDittoDAO;
	}

}
